#include "guiManager.h"


guiManager::guiManager() {

    mouseX = 0;
    mouseY = 0;

    debug = 0;
    flagTypeChat = 0;

    chatLogMax = 511;

    menue[0] = "Information";
    menue[1] = "Profile";
    menue[2] = "Ship Setup";
    menue[3] = "Communication";
    menue[4] = "Market System";
    menue[5] = "Politics";
    menue[6] = "Setup";

    win = NULL;
    msg = NULL;

    sfRadar = NULL;
    sfLogo = NULL;
    sfMenue = NULL;
    sfMenueOver = NULL;
    sfBottom = NULL;
    sfMenueBackgound = NULL;

}


guiManager::~guiManager() {
    //dtor
}

void guiManager::IntroduceMessage(msgManager *pMsg) {
    msg = pMsg;
    if(debug>0) msg->Add("gui: msg linked");
}


void guiManager::IntroduceWindows(windowManager *pWin) {
    win = pWin;
    if(debug>0) msg->Add("gui: win linked");
}


void guiManager::Init(void) {


    // Grafics
    BITMAP *tmp;
    tmp = load_bitmap("GFX/mapMain.bmp", NULL);
    if(!tmp)  {
       cout << "Could not load [GFX/mapMain.bmp]" << endl;
       exit(2);
    }

    sfLogo = create_video_bitmap(34, 128);
    blit(tmp, sfLogo, 0, 0, 0, 0, sfLogo->w, sfLogo->h);

    sfMenue = create_video_bitmap(35, 30);
    blit(tmp, sfMenue, 0, 130, 0, 0, sfMenue->w, sfMenue->h);

    sfMenueOver = create_video_bitmap(35, 30);
    blit(tmp, sfMenueOver, 0, 162, 0, 0, sfMenueOver->w, sfMenueOver->h);

    sfMenueBackgound = create_video_bitmap(35, 2);
    blit(tmp, sfMenueBackgound, 0, 194, 0, 0, sfMenueBackgound->w, sfMenueBackgound->h);

    sfBottom = create_video_bitmap(35, 16);
    blit(tmp, sfBottom, 0, 199, 0, 0, sfBottom->w, sfBottom->h);

    tmp = load_bitmap("GFX/mapWindow.bmp", NULL);
    sfChatFrame = create_video_bitmap(1, 1);
    blit(tmp, sfChatFrame, 267, 1, 0, 0, sfChatFrame->w, sfChatFrame->h);


    destroy_bitmap(tmp);




    sfRadar = load_bitmap("GFX/mapRadar.bmp", NULL);
    if(!sfRadar)  {
       cout << "Could not load [GFX/mapRadar.bmp]" << endl;
       exit(2);
    }




    // Positionen
    int i, e;
    for(i=0;i<=4;i++) {
        for(e=0;e<=14;e++) {
            posWindow[i][e].x = 1+(e*16);
            posWindow[i][e].y = 1+(i*16);
            posWindow[i][e].w = 15;
            posWindow[i][e].h = 15;
        }
        posWindow[i][9].w = 47;

        posWindow[i][10].x = 193;
        posWindow[i][10].w = 15;
    }


    ScreenW = SCREEN_W;
    ScreenH = SCREEN_H;

}



void guiManager::Draw(void) {


    // logo
    blit(sfLogo, screen, 0, 0, 0, 0, sfLogo->w, sfLogo->h);

    // radar
    masked_blit( sfRadar, screen, 0, 0, (ScreenW-200), 0, 200, 200 );


    // rollover menue
    int i, end;
    for(i=0;i<=7;i++) {
        if( (mouseY > (i*30)+ 128) && (mouseY < ( (i+1)*30)+ 128) && mouseX > 0 && mouseX < 35) {
            blit(sfMenue, screen, 0, 0, 0, ((i*30)+ 128), sfMenue->w, sfMenue->h);
            textprintf_ex(screen, font, 50, (((i+1)*30)+ 110), makecol(255, 255, 255), -1, "%s", menue[i].c_str() );
        } else blit(sfMenueOver, screen, 0, 0, 0, ((i*30)+ 128), sfMenueOver->w, sfMenueOver->h);
        end = (((i+1)*30)+ 99 );
    }



    // background
    stretch_blit(sfMenueBackgound,      // source Bitmap
                 screen,                // dest Bitmap
                 0,                     // source x
                 0,                     // source y
                 sfMenueBackgound->w,   // source w
                 sfMenueBackgound->h,   // source h
                 0,                     // dest x
                 end,                   // dest y
                 sfMenueBackgound->w,   // dest w
                 SCREEN_H-end);         // dest h


    // bottom
    blit(sfBottom, screen, 0, 0, 0, (SCREEN_H-sfBottom->h), sfBottom->w, sfBottom->h);



    //**** chatLog
    int x=50, y=ScreenH-15, w=400; // H=fixed

    for(i=0;i<10;i++) {
        textprintf_ex(screen, font, x, y-((i*15))-15, makecol(255, 255, 255), -1, "%s", chatLog[i].c_str() );
    }

    //**** chatBox
    if(flagTypeChat) {

        textprintf_ex(screen, font, x+3, y, makecol(255, 255, 255), -1, "%s", chatInput.c_str() );

        // frame
        stretch_blit(sfChatFrame, screen, 0, 0, sfChatFrame->w, sfChatFrame->h, x, y-3, w, 1);  // Up
        stretch_blit(sfChatFrame, screen, 0, 0, sfChatFrame->w, sfChatFrame->h, x, y+10, w, 1); // Down
        stretch_blit(sfChatFrame, screen, 0, 0, sfChatFrame->w, sfChatFrame->h, x, y-3, 1, 14); // Left
        stretch_blit(sfChatFrame, screen, 0, 0, sfChatFrame->w, sfChatFrame->h, x+w, y-3, 1, 14); // Right
    }


}



void guiManager::InjectMouse(int pMausX, int pMausY) {
    mouseX = pMausX;
    mouseY = pMausY;
}

void guiManager::InjectKeys(int pKey) {
    char cKey = (char)pKey;
    if( cKey>=32 && cKey<=126 ) chatInput = chatInput + cKey;
}

void guiManager::InjectChat(string pMessage) {

    int i;
    for(i=chatLogMax;i>=1;i--) chatLog[i] = chatLog[i-1];
    chatLog[0] = pMessage;

}


bool guiManager::InjectClick(int pClickX, int pClickY) {

    if(pClickX > 0 && pClickX < 35) {
        int i;
        for(i=0;i<7;i++) {
            if( (pClickY > (i*30)+ 128) && (mouseY < ( (i+1)*30)+ 128) ) {
                win->windowOpen(i);
            }
        }

        // Chat Box
        if( pClickY < ScreenH && pClickY > (ScreenH-30) ) return true;
    }
    return false;
}


